package cn.jbolt.admin.msgcenter;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.kit.JBoltUserKit;
import cn.jbolt.core.model.SysNotice;
import cn.jbolt.core.model.Todo;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.service.JBoltSysNoticeReaderService;
import cn.jbolt.core.service.JBoltSysNoticeService;
import cn.jbolt.core.service.JBoltTodoService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheck
@Path("/api/admin/msgCenter")
public class MsgCenterAdminController extends JBoltApiBaseController {
    @Inject
    JBoltSysNoticeService sysNoticeService;
    @Inject
    JBoltTodoService todoService;
    @Inject
    JBoltSysNoticeReaderService sysNoticeReaderService;

    /**
     * 右上角消息中心的 通知数据源
     */
    @Before(JBoltNoUrlPara.class)
    @JBoltHttpGet
    public void sysNoticePortalDatas(){
        renderJBoltApiSuccessWithData(sysNoticeService.getMsgCenterPortalDatas());
    }

    /**
     * 右上角消息中心的 代办事项数据源
     */
    @Before(JBoltNoUrlPara.class)
    @JBoltHttpGet
    public void todoPortalDatas(){
        renderJBoltApiSuccessWithData(todoService.getMsgCenterPortalDatas());
    }

    /**
     * 个人消息中心查看更多列表查询 自己的通知信息列表
     * @param page
     * @param pageSize
     * @param keywords
     * @param sortColumn
     * @param sortType
     * @param readed
     */
    public void sysNoticeDatas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
                               @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
                               @Para(KEYWORDS_PARAM_NAME)String keywords,
                               @Para(value = "sortColumn",defaultValue = "id")String sortColumn,
                               @Para(value = "sortType",defaultValue = "desc")String sortType,
                               @Para("readed")Boolean readed
                               ){
        renderJBoltApiSuccessWithData(sysNoticeService.paginateUserSysNotices(page, pageSize, keywords, JBoltUserKit.getUserId(), sortColumn, sortType,readed, false, true,false, "content"));
    }

    /**
     * 个人消息中心查看更多列表查询 自己的待办信息列表
     * @param page
     * @param pageSize
     * @param keywords
     * @param sortColumn
     * @param sortType
     * @param type
     * @param state
     * @param dateTimeColumn
     */
    public void todoDatas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
                               @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
                               @Para("keywords")String keywords,
                               @Para(value = "sortColumn",defaultValue = "id")String sortColumn,
                               @Para(value = "sortType",defaultValue = "desc")String sortType,
                               @Para("type")Integer type,
                               @Para("state")Integer state,
                               @Para("dateTimeColumn")String dateTimeColumn
    ){
        renderJBoltApiSuccessWithData(todoService.paginateUserTodos(page, pageSize, keywords, JBoltUserKit.getUserId(), sortColumn, sortType, type,state,dateTimeColumn,getDateRange(),false, "content"));
    }

    /**
     * 查看sysNotice详情
     */
    @JBoltHttpGet
    public void sysNoticeDetail(){
        Long id = getLong(0);
        if(notOk(id)){
            renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
            return;
        }
        SysNotice sysNotice = sysNoticeService.findById(id);
        if(sysNotice==null){
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        //校验权限
        Ret ret=sysNoticeService.checkUserHasAuth(JBoltUserKit.getUser(),sysNotice);
        if(ret.isFail()) {
            renderJBoltApiFail(ret.getStr("msg"));
            return;
        }
        sysNoticeService.processReceiverValues(sysNotice);
        Kv datas=Kv.create();
        datas.set("sysNotice",sysNotice);
        //datas.set("files",jBoltFileService.getListByIds(sysNotice.getFiles()));
        datas.set("readed", sysNoticeReaderService.existsReader(id,JBoltUserKit.getUserId()));
        renderJBoltApiSuccessWithData(datas);
    }

    /**
     * 查看todo详情
     */
    @JBoltHttpGet
    public void todoDetail(){
        Long id = getLong(0);
        if(notOk(id)){
            renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
            return;
        }
        Todo todo = todoService.findById(id);
        if(todo==null){
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        Long userId = JBoltUserKit.getUserId();
        if(!todo.getUserId().equals(userId)) {
            renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
        renderJBoltApiSuccessWithData(todo);
    }


    /**
     * 消息中心  通知 标记为已读
     */
    @JBoltHttpGet
    @Before({Tx.class,JBoltNoUrlPara.class})
    public void todoMarkAsRead() {
        renderJBoltApiRet(todoService.markAsRead(getId()));
    }
    /**
     * 消息中心  通知 标记为已读
     */
    @JBoltHttpGet
    @Before({Tx.class,JBoltNoUrlPara.class})
    public void sysNoticeMarkAsRead() {
        renderJBoltApiRet(sysNoticeService.markAsRead(getId()));
    }

    /**
     * 批量标记已读
     */
    @JBoltHttpGet
    @Before({Tx.class,JBoltNoUrlPara.class})
    public void sysNoticeMarkMultiAsRead() {
        renderJBoltApiRet(sysNoticeService.markMultiAsRead(getIds()));
    }

    /**
     * 当前用户
     * 是否显示未读信息红点
     */
    @JBoltHttpGet
    public void showMeUnreadRedDot() {
        boolean needRedDot = sysNoticeService.existUnread(JBoltUserKit.getUserId());
        if(!needRedDot) {
            needRedDot = todoService.existUnread(JBoltUserKit.getUserId());
            if(!needRedDot) {
                needRedDot = todoService.existNeedProcess(JBoltUserKit.getUserId());
            }
        }
        renderJBoltApiSuccessWithData(needRedDot);
    }
}
